/**
 * 
 */
package com.jm.web.builder.form;

import java.io.IOException;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.utils.Utils;
import com.jm.web.builder.BaseBuilder;
import com.jm.web.tags.form.OrgTag;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 *
 * 2011/9/29 下午1:18:00
 */
public class OrgBuilder extends BaseBuilder{
	protected OrgTag orgTag = null;
		
	@Override
	public void init(BaseTag tag) {
		super.init(tag);
		orgTag = (OrgTag)this.getBaseTag();
	}
	
	@Override
	public void RenderEnd() {
		
		int version = this.getBaseTag().GetExtValue("version",1);
		
		Utils utils = this.getBaseTag().getPage().getUtils();
		StringBuilderEx sb = new StringBuilderEx();
		if(this.getBaseTag().isDSP()){
			sb.Append("<field ");
			if(version == 1){
				sb.Append(" name=\"%1$s\"",this.getBaseTag().GetExtValue("name", ""));
			}else{
				sb.Append(" name=\"%1$s_text\"",this.getBaseTag().GetExtValue("name", ""));
			}
			if(this.getBaseTag().IsContainsKey("id"))
				sb.Append(" id=\"%1$s\"",this.getBaseTag().GetExtValue("id", ""));
			
			if(this.getBaseTag().IsContainsKey("captionStyle"))
				sb.Append(" captionStyle=\"%1$s\"",this.getBaseTag().GetExtValue("captionStyle", ""));
			if(this.getBaseTag().IsContainsKey("bodyStyle"))
				sb.Append(" bodyStyle=\"%1$s\"",this.getBaseTag().GetExtValue("bodyStyle", ""));
			if(this.getBaseTag().IsContainsKey("captionWidth"))
				sb.Append(" captionWidth=\"%1$s\"",this.getBaseTag().GetExtValue("captionWidth", ""));
			if(this.getBaseTag().IsContainsKey("captionDisplay"))
				sb.Append(" captionDisplay=\"%1$s\"",this.getBaseTag().GetExtValue("captionDisplay", ""));
			if(this.getBaseTag().IsContainsKey("captionCss"))
				sb.Append(" captionCss=\"%1$s\"",this.getBaseTag().GetExtValue("captionCss", ""));
			if(this.getBaseTag().IsContainsKey("colspan"))
				sb.Append(" colspan=\"%1$s\"",this.getBaseTag().GetExtValue("colspan", ""));
			if(this.getBaseTag().IsContainsKey("caption"))
				sb.Append(" caption=\"%1$s\"",this.getBaseTag().GetExtValue("caption", ""));
			
			if(this.getBaseTag().IsContainsKey("rule"))
				sb.Append(" rule=\"%1$s\"",this.getBaseTag().GetExtValue("rule", ""));
			
			if(this.getBaseTag().IsContainsKey("msg"))
				sb.Append(" msg=\"%1$s\"",this.getBaseTag().GetExtValue("msg", ""));
			
			if(StringHelper.GetValue(this.orgTag.getRequired(), false))
				sb.Append(" required=\"true\"");
			
			if(!StringHelper.IsNullOrEmpty(this.orgTag.getRequiredMsg()))
				sb.Append(" requiredMsg=\"%1$s\"",this.orgTag.getRequiredMsg());
			
			sb.Append(" >");
		}
		
		//顯示的欄位
		sb.Append("<input readonly=\"readonly\" %1$s ",this.getBaseTag().GetExtValue("extAtr", ""));
		
		if(version == 1){
			sb.Append(" name=\"%1$s\"",this.getBaseTag().GetExtValue("name", ""));
			sb.Append(" hdname=\"%1$stsn\"",this.getBaseTag().GetExtValue("name", ""));
		}else{
			sb.Append(" name=\"%1$s_text\"",this.getBaseTag().GetExtValue("name", ""));
			sb.Append(" hdname=\"%1$s\"",this.getBaseTag().GetExtValue("name", ""));
		}
		
		if(this.getBaseTag().IsContainsKey("value"))
			sb.Append(" value=\"%1$s\"",this.getBaseTag().GetExtValue("value", ""));
		
		if(this.getBaseTag().IsContainsKey("version"))
			sb.Append(" version=\"%1$s\"",this.getBaseTag().GetExtValue("version", ""));
		
		if(this.getBaseTag().IsContainsKey("enab"))
			sb.Append(" enab=\"%1$s\"",this.getBaseTag().GetExtValue("enab", ""));
		
		if(this.getBaseTag().IsContainsKey("style"))
			sb.Append(" style=\"%1$s\"",this.getBaseTag().GetExtValue("style", ""));
		
		sb.Append(" caption=\"%1$s\"",this.getBaseTag().GetExtValue("caption", ""));
		
		if(this.getBaseTag().IsContainsKey("skip"))
			sb.Append(" skip=\"%1$s\"",this.getBaseTag().GetExtValue("skip", ""));
		
		Object lock = this.getBaseTag().GetExtValue("lock");
		String strparams = "";
		if(lock != null){
			if(lock instanceof Boolean){
				if((Boolean)lock){
					strparams = StringHelper.Format("hr003atsn:'%1$s'",utils.getsv("companyCodeTsn"));
				}
			}else if(lock instanceof String){
				if("true".equalsIgnoreCase((String)lock))
					strparams = StringHelper.Format("hr003atsn:'%1$s'",utils.getsv("companyCodeTsn"));
				else
					strparams = StringHelper.Format("hr003atsn:'%1$s'",lock);
			}
		}
		
		if(this.getBaseTag().GetExtValue("multiSelect", false)){
			if(strparams.length() > 0)
				strparams += ",";
			
			strparams += "multiselect:true";
		}
		
		
		sb.Append(" onclick=\"var _ret=$P.showOrg(this,{%2$s},%1$s);\"",this.getBaseTag().GetExtValue("onPicked", "''"),strparams);
		
		
		if(this.getBaseTag().GetExtValue("hidden",false))
			sb.Append(" type=\"hidden\"");
		else
			sb.Append(" type=\"text\"");
		
		if(this.getBaseTag().IsContainsKey("id"))
			sb.Append(" id=\"%1$s\"",this.getBaseTag().GetExtValue("id", ""));
		if(this.getBaseTag().IsContainsKey("width"))
			sb.Append(" width=\"%1$s\"",this.getBaseTag().GetExtValue("width", ""));
		if(this.getBaseTag().IsContainsKey("class"))
			sb.Append(" class=\"%1$s\"",this.getBaseTag().GetExtValue("class", ""));
		
		if(this.getBaseTag().IsContainsKey("action"))
			sb.Append(" action=\"%1$s\"",this.getBaseTag().GetExtValue("action", ""));
		
		if(this.getBaseTag().IsContainsKey("actionvalue"))
			sb.Append(" actionvalue=\"%1$s\"",this.getBaseTag().GetExtValue("actionvalue", ""));
		
		sb.Append(" />");
		
		//隱藏的欄位
		sb.Append("<input type=\"hidden\" name=\"%1$s\" value=\"%2$s\" />",
				 StringHelper.Format(version == 1 ? "%1$stsn" : "%1$s",this.getBaseTag().GetExtValue("name", "")),
				this.getBaseTag().GetExtValue("hiddenvalue", "")
				);
		if(this.orgTag.GetExtValue("subOrg",false)){
			sb.Append("<input type=\"checkbox\" name=\"sub%1$s\" value=\"1\" />",this.getBaseTag().GetExtValue("name", ""));
			sb.Append("<span class=\"suborg\" style=\"color:#6495ED;font-size:12px;\">("+utils.i18n("含有子部門")+")</span>");
		}
		
		if(this.getBaseTag().isDSP()){
			sb.Append("</field>");
		}
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}